/*
 * @Author: lerio.cheng
 * @Date:   2017-02-22 16:44:17
 * @Last Modified by:   DELL
 * @Last Modified time: 2017-02-24 12:49:10
 */

'use strict';

app.controller('addHouseCtrltest', function($scope, tipService, $http, rest, $state, ngDialog, user, $location,uploadConf) {
    $scope.params = {
        landlordId: user.get('landlordId'),
        BedRoom: 1,
        LiveRoom: 1,
        KitchenRoom: 1,
        ToiletRoom: 1
    };
    $scope.disable_save_btn = false;
     
           $scope.column2 = 0;
           $scope.maxImgNum = 5;
           $scope.imgNew= [
                {
                    text:'卧室图片',
                    name: 'bedRoom',
                    value: []
                },
                {
                    text:'客厅图片',
                    name: 'livingRoom',
                    value: []
                },
                {
                    text:'卫生间图片',
                    name: 'bathRoom',
                    value: []
                },
                {
                    text:'厨房图片',
                    name: 'kitchenRoom',
                    value: []
                },
                {
                    text:'周边环境',
                    name: 'environment',
                    value: []
                }
           ];   
       
    uploadConf.column2 = $scope.column2;
     uploadConf.maxImgNum =  $scope.maxImgNum;
     uploadConf.imgNew= $scope.imgNew;
    

    // 户型
    $scope.houseType = { selected: {name: '一室户', value: 7} };
    $scope.houseTypeList = [{
        name: '一室户',
        value: 7
    }, {
        name: '一室一厅',
        value: 0
    }, {
        name: '一室半',
        value: 1
    }, {
        name: '二室一厅',
        value: 2
    }, {
        name: '二室半',
        value: 3
    }, {
        name: '三室一厅',
        value: 4
    }, {
        name: '四室以上',
        value: 5
    }, {
        name: '复式',
        value: 6
    }];
    // 朝向4013
    $scope.orientation = {};
    $scope.orientationList = [{
        name: '东',
        value: 0
    }, {
        name: '南',
        value: 1
    }, {
        name: '西',
        value: 2
    }, {
        name: '北',
        value: 3
    }, {
        name: '东南',
        value: 4
    }, {
        name: '西南',
        value: 5
    }, {
        name: '东北',
        value: 6
    }, {
        name: '西北',
        value: 7
    }, {
        name: '南北',
        value: 8
    }];
    // 装修情况
    $scope.renovation = { selected: {name: '简装', value: 1} };
    $scope.renovationList = [{
        name: '毛坯',
        value: 0
    }, {
        name: '简装',
        value: 1
    }, {
        name: '中等装修',
        value: 2
    }, {
        name: '精装修',
        value: 3
    }, {
        name: '豪华装修',
        value: 4
    }];
    // 支付方式
    $scope.payType = {};
    $scope.payTypeList = [{
        name: '一月一付',
        value: 0
    }, {
        name: '二月一付',
        value: 1
    }, {
        name: '三月一付',
        value: 2
    }];

    // 押金
    $scope.deposit = {};
    $scope.depositList = [{
        name: '无押金',
        value: 0
    }, {
        name: '一月押金',
        value: 1
    }, {
        name: '两月押金',
        value: 2
    }, {
        name: '三月押金',
        value: 3
    }];
    // 出租类型
    $scope.leaseType = { selected: {name: '整租', value: 0} };
    $scope.leaseTypeList = [{
        name: '整租',
        value: 0
    }, {
        name: '合租',
        value: 1
    }];
    // 是否已出租
    $scope.isRent = { selected: {name: '未出租', value: 0} };
    $scope.isRentList = [{
            name: '已出租',
            value: 1
        }, {
            name: '未出租',
            value: 0
        }]
        // 房屋设施
    $scope.houseDevice = {};
    $scope.houseDeviceList = [{
        name: '床',
        value: 'Bed'
    }, {
        name: '沙发',
        value: 'Sofa'
    }, {
        name: '餐桌',
        value: 'Board'
    }, {
        name: '书桌',
        value: 'Desk'
    }, {
        name: '椅子',
        value: 'Chair'
    }, {
        name: '洗衣机',
        value: 'WashMachine'
    }, {
        name: '冰箱',
        value: 'Refrigerator'
    }, {
        name: '电视',
        value: 'Television'
    }, {
        name: '空调',
        value: 'AirCondition'
    }, {
        name: '热水器',
        value: 'WaterHeater'
    }, {
        name: '油烟机',
        value: 'LampblackMachine'
    }, {
        name: '橱柜',
        value: 'Ambry'
    }, {
        name: '无线网',
        value: 'Wifi'
    }, {
        name: '燃气灶',
        value: 'Stove'
    }, {
        name: '微波炉',
        value: 'MicrowaveOven'
    }];

    function deletSame(a) {
        if ($scope.params.HousePic) {
            for (var j in $scope.params.HousePic) {
                if ($scope.params.HousePic[j].picType == a) {
                    $scope.params.HousePic.splice(j, 1);
                }
            }
        }
    }
    // 卧室照片
    $scope.params.HousePic = []
    $scope.bedRoom = $scope.bedRoom || [];

    $scope.$on('bedRoomChange', function(d, data) {
        deletSame(0);
        for (var i in data.list) {
            $scope.params.HousePic.push({ picPath: data.list[i], picType: 0, sort: 0 });
        }
    });
    // 客厅照片
    $scope.livingRoom = $scope.livingRoom || [];
    $scope.$on('livingRoomChange', function(d, data) {
        deletSame(1);
        for (var i in data.list) {
            $scope.params.HousePic.push({ picPath: data.list[i], picType: 1, sort: 0 });
        }
    });
    // 卫生间照片
    $scope.bathRoom = $scope.bathRoom || [];
    $scope.$on('bathRoomChange', function(d, data) {
        deletSame(2);
        for (var i in data.list) {
            $scope.params.HousePic.push({ picPath: data.list[i], picType: 2, sort: 0 });
        }
    });

    // 厨房照片
    $scope.kitchenRoom = $scope.kitchenRoom || [];
    $scope.$on('kitchenRoomChange', function(d, data) {
        deletSame(3);
        for (var i in data.list) {
            $scope.params.HousePic.push({ picPath: data.list[i], picType: 3, sort: 0 });
        }
    });

    // 外部照片
    $scope.environment = $scope.environment || [];
    $scope.$on('environmentChange', function(d, data) {
        deletSame(4);
        for (var i in data.list) {
            $scope.params.HousePic.push({ picPath: data.list[i], picType: 4, sort: 0 });
        }
    });
    // 监听省市县
    $scope.$watch('_provinceSelected.selected', function(newValue, oldValue) {
        newValue ? $scope.params.ProvinceId = newValue.id : "";
    })
    $scope.$watch('_citySelected.selected', function(newValue, oldValue) {
        newValue ? $scope.params.CityId = newValue.id : "";
    })
    $scope.$watch('_areaSelected.selected', function(newValue, oldValue) {
        newValue ? $scope.params.CountyId = newValue.id : "";
    })
    $scope.bed = true;
    $scope.sofa = true;
    $scope.board = true;
    $scope.desk = true;
    $scope.chair = true;
    $scope.washMachine = true;
    $scope.refrigerator = true;
    $scope.television = true;
    $scope.airCondition = true;
    $scope.waterHeater = true;
    $scope.lampblackMachine = true;
    $scope.ambry = true;
    $scope.wifi = true;
    $scope.stove = true;
    $scope.microwaveOven = true;

    $scope.$watch('bed',function(n,o){
        $scope.params.bed = n ? 1 : 0;
    });
    $scope.$watch('sofa',function(n,o){ 
        $scope.params.sofa = n ? 1 : 0;
    });
    $scope.$watch('board',function(n,o){ 
        $scope.params.board = n ? 1 : 0;
    });
    $scope.$watch('desk',function(n,o){ 
        $scope.params.desk = n ? 1 : 0;
    });
    $scope.$watch('chair',function(n,o){ 
        $scope.params.chair = n ? 1 : 0;
    });
    $scope.$watch('washMachine',function(n,o){ 
        $scope.params.washMachine = n ? 1 : 0;
    });
    $scope.$watch('refrigerator',function(n,o){ 
        $scope.params.refrigerator = n ? 1 : 0;
    });
    $scope.$watch('television',function(n,o){ 
        $scope.params.television = n ? 1 : 0;
    });
    $scope.$watch('airCondition',function(n,o){ 
        $scope.params.airCondition = n ? 1 : 0;
    });
    $scope.$watch('waterHeater',function(n,o){ 
        $scope.params.waterHeater = n ? 1 : 0;
    });
    $scope.$watch('lampblackMachine',function(n,o){ 
        $scope.params.lampblackMachine = n ? 1 : 0;
    });
    $scope.$watch('ambry',function(n,o){ 
        $scope.params.ambry = n ? 1 : 0;
    });
    $scope.$watch('wifi',function(n,o){ 
        $scope.params.wifi = n ? 1 : 0;
    });
    $scope.$watch('stove',function(n,o){ 
        $scope.params.stove = n ? 1 : 0;
    });
    $scope.$watch('microwaveOven',function(n,o){ 
        $scope.params.microwaveOven = n ? 1 : 0;
    });
    $scope.chooseAll = function(n,o){
        if ($scope.params.bed == 1) {
            $scope.params.bed = 0;
            $scope.bed = false;
        }else if($scope.params.bed == 0) {
            $scope.params.bed = 1;
            $scope.bed = true;
        }
        if ($scope.params.sofa == 1) {
            $scope.params.sofa = 0;
            $scope.sofa = false;
        }else if($scope.params.sofa == 0) {
            $scope.params.sofa = 1;
            $scope.sofa = true;
        }
        if ($scope.params.board == 1) {
            $scope.params.board = 0;
            $scope.board = false;
        }else if($scope.params.board == 0) {
            $scope.params.board = 1;
            $scope.board = true;
        }
        if ($scope.params.desk == 1) {
            $scope.params.desk = 0;
            $scope.desk = false;
        }else if($scope.params.desk == 0) {
            $scope.params.desk = 1;
            $scope.desk = true;
        }
        if ($scope.params.chair == 1) {
            $scope.params.chair = 0;
            $scope.chair = false;
        }else if($scope.params.chair == 0) {
            $scope.params.chair = 1;
            $scope.chair = true;
        }
        if ($scope.params.washMachine == 1) {
            $scope.params.washMachine = 0;
            $scope.washMachine = false;
        }else if($scope.params.washMachine == 0) {
            $scope.params.washMachine = 1;
            $scope.washMachine = true;
        }
        if ($scope.params.refrigerator == 1) {
            $scope.params.refrigerator = 0;
            $scope.refrigerator = false;
        }else if($scope.params.refrigerator == 0) {
            $scope.params.refrigerator = 1;
            $scope.refrigerator = true;
        }
        if ($scope.params.television == 1) {
            $scope.params.television = 0;
            $scope.television = false;
        }else if($scope.params.television == 0) {
            $scope.params.television = 1;
            $scope.television = true;
        }
        if ($scope.params.airCondition == 1) {
            $scope.params.airCondition = 0;
            $scope.airCondition = false;
        }else if($scope.params.airCondition == 0) {
            $scope.params.airCondition = 1;
            $scope.airCondition = true;
        }
        if ($scope.params.waterHeater == 1) {
            $scope.params.waterHeater = 0;
            $scope.waterHeater = false;
        }else if($scope.params.waterHeater == 0) {
            $scope.params.waterHeater = 1;
            $scope.waterHeater = true;
        }
        if ($scope.params.lampblackMachine == 1) {
            $scope.params.lampblackMachine = 0;
            $scope.lampblackMachine = false;
        }else if($scope.params.lampblackMachine == 0) {
            $scope.params.lampblackMachine = 1;
            $scope.lampblackMachine = true;
        }
        if ($scope.params.ambry == 1) {
            $scope.params.ambry = 0;
            $scope.ambry = false;
        }else if($scope.params.ambry == 0) {
            $scope.params.ambry = 1;
            $scope.ambry = true;
        }
        if ($scope.params.wifi == 1) {
            $scope.params.wifi = 0;
            $scope.wifi = false;
        }else if($scope.params.wifi == 0) {
            $scope.params.wifi = 1;
            $scope.wifi = true;
        }
        if ($scope.params.stove == 1) {
            $scope.params.stove = 0;
            $scope.stove = false;
        }else if($scope.params.stove == 0) {
            $scope.params.stove = 1;
            $scope.stove = true;
        }
        if ($scope.params.microwaveOven == 1) {
            $scope.params.microwaveOven = 0;
            $scope.microwaveOven = false;
        }else if($scope.params.microwaveOven == 0) {
            $scope.params.microwaveOven = 1;
            $scope.microwaveOven = true;
        }
    }
    var flag = true;
    if (flag) {
        $scope.saveData = function() {
            $scope.disable_save_btn = true;

            flag = false;
            if (!$scope._provinceSelected.selected || !$scope._citySelected.selected || !$scope._areaSelected.selected) {
                tipService.show("body", {
                    message: "所在城市地址必须填全",
                    notice: "danger"
                }, 3);
                return;
            }
            if (!$scope.orientation.selected || !$scope.params.HouseSize) {
                tipService.show("body", {
                    message: "朝向、面积均不能为空",
                    notice: "danger"
                }, 3);
                return;
            }
            if (!$scope.renovation.selected) {
                tipService.show("body", {
                    message: "装修情况不能为空",
                    notice: "danger"
                }, 3);
                return;
            }

            if (!$scope.params.CurrFloor || !$scope.params.Floors || $scope.params.CurrFloor<=0 || $scope.params.Floors<=0 || $scope.params.Floors<$scope.params.CurrFloor) {
                tipService.show("body", {
                    message: "层高、总楼层数格式不正确",
                    notice: "danger"
                }, 3);
                return;
            }
            if (!$scope.leaseType.selected || !$scope.params.HouseRent || !$scope.isRent.selected || !$scope.payType.selected || !$scope.deposit.selected) {
                tipService.show("body", {
                    message: "出租信息栏信息未填全",
                    notice: "danger"
                }, 3);
                return;
            };
            $scope.params.orientation = $scope.orientation.selected.value;
            $scope.params.renovation = $scope.renovation.selected.value;
            $scope.params.payType = $scope.payType.selected.value;
            $scope.params.deposit = $scope.deposit.selected.value;
            $scope.params.leaseType = $scope.leaseType.selected.value;
            $scope.params.isRent = $scope.isRent.selected.value;
            $scope.params.houseType = $scope.houseType.selected.value;
            $scope.params.IsAnyTime = 1; // 随时可入住

            for (var i in $scope.houseDevice.selected) {
                $scope.params[$scope.houseDevice.selected[i].value] = 1;
            }
            rest.post({
                methodId: '4007',
                data: $scope.params,
                success: function(res) {
                    if (res.code == 0) {
                        tipService.show("body", {
                            message: "房源数据提交成功,跳转中......"
                        }, 3);
                        $location.path('/app/houseOwner/list');
                        console.log($scope.params);
                        flag = true;
                    } else {
                        tipService.show("body", {
                            message: "房源数据提交失败!",
                            notice: "danger"
                        }, 3);
                    }
                    $scope.disable_save_btn = false;
                },
                error: function() {
                    $scope.disable_save_btn = false;
                }
            })
        }
    }
    $scope.backList = function() {
        $scope.params = {};
    }

})
